var websocket = null;
var name="null";
var id=0;
var op_name="";
var op_id=0;



//getId
function getCookie(sName)
			{
				
			var aCookie = document.cookie.split(";");
			for (var i=0; i < aCookie.length; i++)
			{
			var aCrumb = aCookie[i].split("=");
			if (sName == aCrumb[0])
			return unescape(aCrumb[1]);
				}
			return null;
}
//getName
function GetRequestName() {
	  
	  var url = location.search; 
	   var theRequest = new Object();
	   if (url.indexOf("?")!= -1) {
	     url=url.substr(url.indexOf("name=")+5);
	   }
	   return url;
	}
function init(){
	
	 id=getCookie("user");
	 
	  //获取所有用户列表
     let aup=new AjaxUtils;
	 let paramp="usermessageadmin.servlet?param=get";
     aup.ajaxRequest(paramp,(xhr)=>{
    	 
    	 
    	 let json=JSON.parse(xhr.responseText);
    	let list=json.userMessage;
    	for(let i=0;i<list.length;i++){
    		let div=document.createElement("div");
    		div.className="friendsub";
    		let img=document.createElement("img");
    		img.src="usermessageadmin.servlet?param=getIcon&id="+list[i].id;
    		img.className="newfriendHeadIcon";
    		let div2=document.createElement("div");
    		div2.innerHTML=list[i].nikname;
    		let button=document.createElement("button");
    		button.type="button";
    		button.className="addFriend";
    		button.id="addFriend"+list[i].id;
    		button.innerHTML="添加";
    		button.addEventListener("click",()=>{
    			button.innerHTML="已申请";
    			let requestString="usermessageadmin.servlet?param=addfriend&id="+id+"&fid="+list[i].id;
    			aup.ajaxRequest(requestString,()=>{
    				
    			})
    			
    			
    		},false)
    		div.appendChild(img);
    		div.appendChild(div2);
    		div.appendChild(button);
    		document.getElementById("friendpading").appendChild(div);
    	}
    	 
    	 
    	 
     });
     
     
	 //获取好友列表
	 let au=new AjaxUtils;
	 let param="chat.servlet?param=getFriends&id="+id;
     au.ajaxRequest(param,(xhr)=>{
     		let json=JSON.parse(xhr.responseText);
     		let list=json.list;
     		for(let i=0;i<list.length;i++){
     			document.getElementById("addFriend"+list[i].id).innerHTML="已添加"
     			let div1=document.createElement("div");
     			div1.className="chat";
     			div1.id="chat"+list[i].id;
     			let div2=document.createElement("div");
     			div2.className="head";
     			div2.id="head"+list[i].id;
     			div2.innerHTML="<font class='headname' id='headname"+list[i].id+"'>"+list[i].nikname+"</font>";
     			div2.addEventListener("click",()=>{
     				document.getElementById("list").style.display="block";
     				document.getElementById("chat"+list[i].id).style.display="none";
     			},false);
     			let div3=document.createElement("div");
     			div3.className="chating";
     			div3.id="chating"+list[i].id+"";
     			let div4=document.createElement("div");
     			div4.className="inputbox";
     			div4.id="inputbox"+list[i].id+"";
     			div4.innerHTML="<input type='text' class='textbox' id='textbox"+list[i].id+"'><img class='sendbutton' onclick='send()' src='images/28.jpg' >"
     			div1.appendChild(div2);
     			div1.appendChild(div3);
     			div1.appendChild(div4);
     			document.getElementById("single").appendChild(div1);
			    let div=document.createElement("div");
     			div.className="listsub";
     			div.id="btn_"+list[i].id;
     			div.addEventListener('click',()=>{
     				document.getElementById("list").style.display="none";
     				document.getElementById("chat"+list[i].id).style.display="block";
     				op_name=list[i].userName;
     				op_id=list[i].id;
     			},false);
     			div.innerHTML="<img class='friendHeadIcon' src='usermessageadmin.servlet?param=getIcon&id="+list[i].id+"'> <div class='friendName'>"+list[i].nikname+"</div><div class='newmessage' id='newmessage"+list[i].id+"'></div>";
     			document.getElementById("listpoint").appendChild(div);
     			
     		}
     		

     	});
 
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
	getname();
}




function getname(){
	
	name=GetRequestName();
	connect(name)
}





function connect(name){
   
    
    if ('WebSocket' in window) {
    	let url="ws://172.7.41.152:8088/blogserver/websocket/"+name;
        websocket = new WebSocket(url);
       
    }
    else {
        alert('当前浏览器 Not support websocket')
    }

   
    websocket.onerror = function () {
        setMessageInnerHTML("WebSocket连接发生错误");
    };

  
    websocket.onopen = function () {
    
    }

 
    websocket.onmessage = function (event) {
        let json=JSON.parse(event.data);
        let newmessage="newmessage"+json.obid;
        document.getElementById(newmessage).innerHTML=json.message;
        let chatting="chating"+json.obid;
        document.getElementById(chatting).innerHTML=document.getElementById(chatting).innerHTML+"<div class='other'><div class='otherimg'><img  class='headimg' src='usermessageadmin.servlet?param=getIcon&id="+json.obid+"'></div><div class='othername'>"+json.ob+"</div><div class='otherchat'>"+json.message+"</div></div>";
    }

   
    websocket.onclose = function () {
       
    }

    
    window.onbeforeunload = function () {
        closeWebSocket();
    }
    
}
   
   

    
    function closeWebSocket() {
        websocket.close();
    }

    
    function send() {
    	
    	var message = document.getElementById("textbox"+op_id).value;
    	document.getElementById('chating'+op_id).innerHTML=document.getElementById('chating'+op_id).innerHTML+"<div class='order'><div class='orderimg'><img  class='headimg' src='usermessageadmin.servlet?param=getIcon&id="+id+"'></div><div class='orderchat'>"+message+"</div></div>";
    	document.getElementById('textbox'+op_id).value="";
    	var op = op_name;
        if(op==""){
        	op="all";
        }
       var map={};
       map['op']=op;
       map['message']=message;
       map['ob']=name
       map['obid']=id;
       jsonstr=JSON.stringify(map);
       websocket.send(jsonstr);
        
    }
  
    window.addEventListener("load",init,false);